package cn.elricyu.tt.config;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;

import cn.elricyu.tt.bean.battle.Power;
import cn.elricyu.tt.bean.battle.multi.MultiBattle;

public class BattleConfig {

	private static DefaultConfig confIns = new DefaultConfig();

	private static Map<Integer, Power> POWER_MAP = null;

	private static Map<Integer, MultiBattle> BATTLE_MAP = null;

	private BattleConfig() {
	}

	public static Map<Integer, MultiBattle> getMultiBattleMap() {
		if (BATTLE_MAP == null || BATTLE_MAP.isEmpty()) {
			synchronized (BattleConfig.class) {
				if (BATTLE_MAP == null || BATTLE_MAP.isEmpty()) {
					BATTLE_MAP = new LinkedHashMap<Integer, MultiBattle>();
					List<String> indexList = confIns
							.getList("battles.battle.index");
					List<String> idList = confIns.getList("battles.battle.id");
					List<String> paramsList = confIns
							.getList("battles.battle.name");
					List<String> descList = confIns
							.getList("battles.battle.rule");
					List<String> poweridList = confIns
							.getList("battles.battle.powerid");
					if (idList != null && idList.size() > 0) {
						for (int i = 0; i < idList.size(); i++) {
							int index = Integer.parseInt(indexList.get(i));
							String id = idList.get(i);
							String name = paramsList.get(i);
							String rule = descList.get(i);
							String powerid = poweridList.get(i);
							MultiBattle actionBean = new MultiBattle(index, id,
									name, rule, powerid);
							BATTLE_MAP.put(index, actionBean);
						}
					}
				}
			}
		}
		return BATTLE_MAP;
	}

	public static Map<Integer, Power> getPowerMap() {
		if (POWER_MAP == null || POWER_MAP.isEmpty()) {
			synchronized (BattleConfig.class) {
				if (POWER_MAP == null || POWER_MAP.isEmpty()) {
					POWER_MAP = new LinkedHashMap<Integer, Power>();
					List<String> indexList = confIns
							.getList("powers.power.index");
					List<String> idList = confIns.getList("powers.power.id");
					List<String> paramsList = confIns
							.getList("powers.power.name");
					if (idList != null && idList.size() > 0) {
						for (int i = 0; i < idList.size(); i++) {
							int index = Integer.parseInt(indexList.get(i));
							int id = Integer.parseInt(idList.get(i));
							String name = paramsList.get(i);
							final Power actionBean = new Power(index, id, name);
							POWER_MAP.put(id, actionBean);
						}
					}
				}
			}
		}
		return POWER_MAP;
	}

	public static Power clonePower(int powerId) {
		Power power = getPowerMap().get(powerId);
		if (power == null) {
			power = new Power(-1, powerId, null);
		}
		return power.clone();
	}

	@SuppressWarnings("unused")
	private boolean getBoolean(String key, boolean defaultValue) {
		String v = RemoteConfig.getString(key, null);
		if (v == null) {
			v = getString(key, "");
		}
		if (StringUtils.isBlank(v)) {
			return defaultValue;
		}
		return ("true".equalsIgnoreCase(v) || "1".equals(v));
	}

	private String getString(String key, String defaultValue) {
		String value = RemoteConfig.getString(key, null);
		if (value == null) {
			value = confIns.getString(key, defaultValue);
		}
		return value;
	}

	@SuppressWarnings("unused")
	private int getInt(String key, int defaultValue) {
		int value = RemoteConfig.getInt(key, -100);
		if (value == -100) {
			value = confIns.getInt(key, defaultValue);
		}
		return value;
	}

	@SuppressWarnings({ "rawtypes", "unused" })
	private List getList(String key) {
		List value = RemoteConfig.getList(key);
		if (value == null || value.size() <= 0) {
			try {
				value = confIns.getList(key);
			} catch (Exception e) {
			}
		}
		return value;
	}

	public static Power getPowerByIndex(int nextIndex) {
		getPowerMap();
		for (Entry<Integer, Power> entry : getPowerMap().entrySet()) {
			Power power = entry.getValue();
			if (nextIndex == power.getIndex()) {
				return power;
			}
		}
		return null;
	}
	
	public static MultiBattle getMultiBattleByIndex(int index) {
		return getMultiBattleMap().get(index);
	}
}
